/*
 * @LastEditTime: 2023-03-31 23:40:38
 * @Method: 
 * @Description: 
 */
package CodeTop;

public class Solution21 {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode l1 = list1, l2 = list2;
        ListNode dummy = new ListNode();
        ListNode pre = dummy;
        while (l1 != null && l2 != null) {
            if (l1.val <= l2.val) {
                pre.next = l1;
                l1 = l1.next;
            } else {
                pre.next = l2;
                l2 = l2.next;
            }
            pre = pre.next;
        }
        while (l1 != null) {
            pre.next = l1;
            l1 = l1.next;
            pre = pre.next;
        }
        while (l2 != null) {
            pre.next = l2;
            l2 = l2.next;
            pre = pre.next;
        }
        pre.next = null;
        return dummy.next;
    }
}
